<template>
  <div class="parent d-flex jc-start ai-center">
    <div class="left pr-10 mr-50">
      <el-form label-width="80px">
        <div class="d-flex flex-column jc-center ai-center mb-100">
          <UploadImg @updateFile="handleUpload" />
          <div class="mt-20">{{$t('renlianbidui.uploadFaceToSeach')}}</div>
        </div>
        <el-form-item :label="$t('renlianbidui.threshold') + ':'">
          <ShowTooltipSlider
            class="slider"
            :min="0"
            :max="100"
            v-model="formData.threshold"
            @change="handleCompare()"
            :show-tooltip="false"
          ></ShowTooltipSlider>
        </el-form-item>
        <el-form-item :label="$t('renlianbidui.lib_name') + ':'"  prop="lib_ids">
          <el-select v-model="formData.lib_ids" @change="handleCompare()" multiple collapse-tags>
            <el-option v-for="item in libList" :key="item.id" :label="item.label" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="TOP-N:" prop="topk">
          <el-select v-model="formData.topk" @change="handleCompare()">
            <el-option
              v-for="item in topkList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="divider"></div>
    <div class="right p-50 flex-1 d-flex flex-column jc-start ai-start">
      <div class="right-top d-flex jc-around ai-center flex-1">
        <MyImg
          v-for="item in staffList"
          :src="item.img_path"
          :key="item.img_path"
          @click.native="handleCompare(item)"
          class="mr-20 my-img"
          style="cursor:pointer"
        />
      </div>
      <div class="h-divider"></div>
      <div class="right-bottom flex-1 d-flex jc-start ai-center">
        <StaffCard
          class="mr-50 mb-20"
          v-for="(item,index) in topnList"
          :cardData="item"
          :key="index"
          style="cursor:pointer"
        />
      </div>
    </div>
  </div>
</template>

<script>
import UploadImg from "@/components/UploadImg";
import { compare } from "../oneToN/service";
import { getLibs } from "../service";
import { getStaff } from "./service";
import { MOSHENGREN_ID } from "@/utils/CONSTANT";
import StaffCard from "../StaffCard";
import ShowTooltipSlider from "../ShowTooltipSlider";
import MyImg from "@/components/Image";
import { imageUrlToFile } from "@/services";
export default {
  components: { UploadImg, StaffCard, ShowTooltipSlider, MyImg },
  data() {
    return {
      formData: {
        threshold: 70,
        topk: 10
      },
      libList: [{ label: this.$t('route.moshengrenku'), id: MOSHENGREN_ID }],
      topkList: [
        { label: "10", value: 10 },
        { label: "20", value: 20 },
        { label: "30", value: 30 },
        { label: "40", value: 40 },
        { label: "50", value: 50 }
      ],
      topnList: [],
      staffList: []
    };
  },
  mounted() {
    this.getLibs();
  },
  methods: {
    async handleCompare(item) {
      if (item) {
        this.currentFile = await imageUrlToFile(item.img_path);
      }
      if (!this.currentFile) return;
      const params = {
        ...this.formData,
        threshold: 100 - this.formData.threshold,
        img: this.currentFile,
        n_topk: 1, // 未找到来源，暂时写死
        lib_ids: this.formData.lib_ids.join(",")
      };
      const res = await compare(params);
      this.topnList = res.data.topn;
    },
    async getLibs() {
      const res = await getLibs();
      res.data = res.data.map(item => {
        return { label: item.lib_name, id: item.lib_id };
      });
      this.libList = this.libList.concat(res.data);
      this.$set(
        this.formData,
        "lib_ids",
        this.libList.map(item => item.id)
      );
    },
    async handleUpload(file) {
      const params = {
        img: file
      };
      const res = await getStaff(params);
      this.staffList = res.data;
      this.$set(this, "topnList", []);
      this.currentFile = void 0;
    }
  }
};
</script>

<style lang="less" scoped>
.parent {
  padding-left: 50px;
  padding-right: 50px;
  .inline {
    width: 230px;
    position: relative;
    .userTooltip {
      background: transparent;
      color: #000 !important;
      width: 60px;
      margin-left: -30px;
      border-radius: 4px;
      text-align: center;
      padding: 8px 0;
      position: absolute;
      top: -30px;
      .navs {
        height: 0;
        width: 0;
        border: 5px solid transparent;
        border-top-color: #fff;
        position: absolute;
        bottom: -9px;
        left: 50%;
        margin-left: -5px;
        z-index: 99;
      }
    }
  }
  .left {
    .slider {
      //   margin-left: 50px;
    }
  }
  .right {
    height: 100%;
    .h-divider {
      width: 100%;
      height: 2px;
      background-color: rgba(217, 222, 229, 1);
      margin-top:30px;
      margin-bottom:30px;
      flex-shrink: 0;
    }
    &-top {
      img {
        width: 165px;
        height: 250px;
      }
      .my-img {
        transition: 0.1s;
        &:hover {
          scale: 1.2;
          background: #ececec;
        }
      }
    }
    &-bottom {
      flex-wrap: wrap;
    }
  }
  .divider {
    background-color: rgba(217, 222, 229, 1);
    width: 2px;
    height: 845px;
    margin-left: 80px;
  }
}
</style>